package com.example.agriapp.model;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.example.agriapp.model.bean.FeedItem;

import java.util.ArrayList;
import java.util.List;

public class FeedModel {
    private final Context context;
    private ContentResolver contentResolver;

    public FeedModel(Context context) {
        this.context = context.getApplicationContext();
       this.contentResolver = context.getContentResolver();
    }

    public List<FeedItem> getLatestFeeds() {
        List<FeedItem> feeds = new ArrayList<>();
        Cursor cursor = this.contentResolver.query(FeedContract.CONTENT_URI,
                null, null, null,
                FeedContract.FeedEntry.COLUMN_DATE + " DESC");

        if (cursor != null) {
            while (cursor.moveToNext()) {
                feeds.add(cursorToFeedItem(cursor));
            }
            cursor.close();
        }

        return feeds;
    }

    public List<FeedItem> getFollowedFeeds() {
        List<FeedItem> feeds = new ArrayList<>();
        String selection = FeedContract.FeedEntry.COLUMN_IS_FOLLOWED + " = ?";
        String[] selectionArgs = new String[]{"1"};
        
        Cursor cursor = this.contentResolver.query(FeedContract.CONTENT_URI,
                null, selection, selectionArgs,
                FeedContract.FeedEntry.COLUMN_DATE + " DESC");
    
        if (cursor != null) {
            while (cursor.moveToNext()) {
                feeds.add(cursorToFeedItem(cursor));
            }
            cursor.close();
        }

        return feeds;
    }

    private FeedItem cursorToFeedItem(Cursor cursor) {
        return new FeedItem(
                cursor.getString(cursor.getColumnIndex(FeedContract.FeedEntry._ID)),
                cursor.getString(cursor.getColumnIndex(FeedContract.FeedEntry.COLUMN_USER_ID)),
                cursor.getString(cursor.getColumnIndex(FeedContract.FeedEntry.COLUMN_USERNAME)),
                cursor.getString(cursor.getColumnIndex(FeedContract.FeedEntry.COLUMN_AVATAR)),
                cursor.getString(cursor.getColumnIndex(FeedContract.FeedEntry.COLUMN_CONTENT)),
                cursor.getString(cursor.getColumnIndex(FeedContract.FeedEntry.COLUMN_DATE)),
                cursor.getInt(cursor.getColumnIndex(FeedContract.FeedEntry.COLUMN_LIKES)),
                cursor.getInt(cursor.getColumnIndex(FeedContract.FeedEntry.COLUMN_COMMENTS)),
                cursor.getInt(cursor.getColumnIndex(FeedContract.FeedEntry.COLUMN_HAS_SHOP)) == 1,
                cursor.getInt(cursor.getColumnIndex(FeedContract.FeedEntry.COLUMN_IS_FOLLOWED)) == 1,
                cursor.getString(cursor.getColumnIndex(FeedContract.FeedEntry.COLUMN_TAG)),
                cursor.getString(cursor.getColumnIndex(FeedContract.FeedEntry.COLUMN_IMAGE_URI))
        );
    }
    public void likeFeed(String feedId) {
        updateLikeCount(feedId, 1);
    }

    public void updateLikeCount(String feedId, int delta) {
        ContentValues values = new ContentValues();
        values.put(FeedContract.FeedEntry.COLUMN_LIKES,
                FeedContract.FeedEntry.COLUMN_LIKES + " + " + delta);

        this.contentResolver.update(ContentUris.withAppendedId(FeedContract.CONTENT_URI, Long.parseLong(feedId)),
                values, null, null);
    }
    public void unlikeFeed(String feedId) {
        updateLikeCount(feedId, -1);
    }

    public void followUser(String userId) {
        updateFollowStatus(userId, true);
    }

    public void unfollowUser(String userId) {
        updateFollowStatus(userId, false);
    }

    // 自定义方法：更新关注状态
    public void updateFollowStatus(String userId, boolean follow) {
        ContentValues values = new ContentValues();
        values.put(FeedContract.FeedEntry.COLUMN_IS_FOLLOWED, follow ? 1 : 0);

        this.contentResolver.update(FeedContract.CONTENT_URI, values,
                FeedContract.FeedEntry.COLUMN_USER_ID + " = ?", new String[]{userId});
    }

    public void postNewFeed(FeedItem newFeed) {
        insertFeed(newFeed);
    }

    public void insertFeed(FeedItem feed) {
        ContentValues values = new ContentValues();
        values.put(FeedContract.FeedEntry._ID, feed.getId());
        values.put(FeedContract.FeedEntry.COLUMN_USER_ID, feed.getUserId());
        values.put(FeedContract.FeedEntry.COLUMN_USERNAME, feed.getUsername());
        values.put(FeedContract.FeedEntry.COLUMN_AVATAR, feed.getAvatar());
        values.put(FeedContract.FeedEntry.COLUMN_CONTENT, feed.getContent());
        values.put(FeedContract.FeedEntry.COLUMN_DATE, feed.getDate());
        values.put(FeedContract.FeedEntry.COLUMN_LIKES, feed.getLikes());
        values.put(FeedContract.FeedEntry.COLUMN_COMMENTS, feed.getComments());
        values.put(FeedContract.FeedEntry.COLUMN_HAS_SHOP, feed.hasShop());
        values.put(FeedContract.FeedEntry.COLUMN_IS_FOLLOWED, feed.isFollowed());
        values.put(FeedContract.FeedEntry.COLUMN_TAG, feed.getTag());
        values.put(FeedContract.FeedEntry.COLUMN_IMAGE_URI, feed.getImageUri());

        this.contentResolver.insert(FeedContract.CONTENT_URI, values);
    }

}